package com.pyip.reggie.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyip.reggie.Entity.Employee;
import com.pyip.reggie.common.R;
import com.pyip.reggie.mapper.EmployeeMapper;
import com.pyip.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        R<Employee> rAns = new R<>();
        // 根据employee的username查找信息，不存在则返回null
        QueryWrapper<Employee> qw = new QueryWrapper<>();
        qw.eq("username",employee.getUsername());
        // username是unique属性的
        Employee ans = employeeMapper.selectOne(qw);
        if(ans == null){
            return rAns.error("用户不存在");
        }
        // 匹配密码
        if(!DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()).equals(ans.getPassword())){
            // 密码不匹配，返回R,error
            return rAns.error("密码不正确");
        }
        // 匹配是否可用
        if(ans.getStatus() == 0){
            return rAns.error("此用户当前不可用");
        }
        // 存在用户且可用且密码正确
        request.getSession().setAttribute("employee",ans.getId());
        return rAns.success(ans);
    }

    @Override
    public R<String> addNewEmployee(HttpServletRequest request, Employee employee) {
        // 添加新员工
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        HttpSession session = request.getSession();
//
//        employee.setCreateUser((Long)session.getAttribute("employee"));
//        employee.setUpdateUser((Long)session.getAttribute("employee"));
        int row = baseMapper.insert(employee);

        return row == 1 ? R.success("添加成功") : R.error("添加失败") ;
    }

    @Override
    public R<Page<Employee>> getEmployeePage(int page, int pageSize, String name) {
        Page<Employee> pageInfo = new Page<>();
        // name可能存在也可能不存在
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
//        添加条件
        lqw.like(!StringUtils.isEmpty(name),Employee::getName,name); // 当第一个参数为true的时候，将后面的东西进行封装，否则不封装
        // 添加排序条件
        lqw.orderByDesc(Employee::getCreateTime);
        // 执行查询
        employeeMapper.selectPage(pageInfo,lqw); // 执行成功后的数据会封装到pageInfo中
        return R.success(pageInfo);
    }

    @Override
    public R<String> updateEmployeeById(HttpServletRequest request, Employee employee) {
        // 更新数据
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long)request.getSession().getAttribute("employee"));
        int row = baseMapper.updateById(employee);
        return row == 1?R.success("更新数据成功") : R.error("更新失败");
    }
}
